package com.taotie.service.impl;

import com.taotie.context.BaseContext;
import com.taotie.dto.ShoppingCartDTO;
import com.taotie.entity.Dish;
import com.taotie.entity.Setmeal;
import com.taotie.entity.ShoppingCart;
import com.taotie.mapper.DishMapper;
import com.taotie.mapper.SetmealMapper;
import com.taotie.mapper.ShoppingCartMapper;
import com.taotie.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断当前商品是否加入了购物车
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if (shoppingCartList != null && shoppingCartList.size() == 1) {
            //执行更新操作，更新商品数量
            shoppingCart = shoppingCartList.get(0);
            Integer number = shoppingCart.getNumber();
            shoppingCart.setNumber(number + 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        } else {
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            } else {
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }


    }

    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //获取当前用户id,查询当前购物车信息
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> userList = shoppingCartMapper.list(shoppingCart);
       if(userList != null && !userList.isEmpty()){
           shoppingCart = userList.get(0);
           Integer number =shoppingCart.getNumber();
           //商品仅剩1时则直接删除
           if(number == 1){
               Long id = shoppingCart.getId();
               shoppingCartMapper.deleteById(id);
           } else {
              //商品大于1则执行修改操作
               shoppingCart.setNumber(number - 1);
               shoppingCartMapper.updateNumberById(shoppingCart);
           }


       }




    }
}
